#include "ui_camera.h"

lv_obj_t *back_btn;
lv_obj_t *photo_vedio_btn;
lv_obj_t *image_btn;
lv_obj_t *image_prev_btn;
lv_obj_t *image_next_btn;
lv_obj_t *ui_container;

// 添加全局变量来跟踪界面状态
enum UI_STATE {
    UI_STATE_CAMERA,
    UI_STATE_IMAGE_VIEW
};
static enum UI_STATE current_ui_state = UI_STATE_CAMERA;

extern int current_photo_index;
extern int total_photos;

void show_current_photo(void)
{
    char photo_path[256];
    printf("current_photo_index = %d\n", current_photo_index);
    snprintf(photo_path, sizeof(photo_path), "/mywork/img_rgb/photo_%d.rgb", current_photo_index); 
    display_photo(photo_path, 640, 480);
}

static void back_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        printf("back_event_cb clicked!\n");
        close_v4l2_camera();
        _ui_screen_change(&ui_ScreenDestop, LV_SCR_LOAD_ANIM_FADE_ON, 0, 0, &ui_ScreenDestop_screen_init);
    }
}

// pthread_mutex_t camera_mutex = PTHREAD_MUTEX_INITIALIZER;
extern int capture_image;
static void photo_vedio_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_SHORT_CLICKED)
    {
        printf("photo_vedio_event_cb PRESSED!\n");

        // pthread_mutex_lock(&camera_mutex);
        capture_image = 1;
        // pthread_mutex_unlock(&camera_mutex);

    }
}

// 查看照片
static void check_image_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        printf("check_image_event_cb clicked!\n");
        
        if (current_ui_state == UI_STATE_CAMERA) {
            // 从拍照界面进入查看图片界面
            ui_check_image();
            current_ui_state = UI_STATE_IMAGE_VIEW;
        } else {
            // 从查看图片界面返回拍照界面
            return_to_camera();
            current_ui_state = UI_STATE_CAMERA;
        }
    }
}

static void prev_image_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        printf("prev_image_event_cb clicked!\n");
        if (current_photo_index > 0)
        {
            current_photo_index--;
            printf("current_photo_index = %d\n", current_photo_index);
            show_current_photo();
        }
    }
}

static void next_image_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        printf("next_image_event_cb clicked!\n");
        if (current_photo_index < total_photos - 1)
        {
            current_photo_index++;
            printf("current_photo_index = %d\n", current_photo_index);
            show_current_photo();
        }
    }
}

// 新增函数：返回拍照界面
void return_to_camera(void)
{
    // 清除查看图片界面的UI元素
    if (image_prev_btn) {
        lv_obj_del(image_prev_btn);
        image_prev_btn = NULL;
    }
    
    if (image_next_btn) {
        lv_obj_del(image_next_btn);
        image_next_btn = NULL;
    }
    
    if (image_btn) {
        lv_obj_del(image_btn);
        image_btn = NULL;
    }
    
    // 清除屏幕
    clear_area(0, 0, 1024, 600, lv_color_hex(0x000000));
    lv_refr_now(NULL);
    
    // 重新创建拍照界面的UI
    ui_app_camera();
}

void ui_app_camera(void)
{
    // 重新初始化摄像头
    if (ds_v4l2_camera_init() != 0) {
        printf("Failed to reinitialize camera\n");
        return;
    }

    // 创建透明背景的UI容器
    ui_container = lv_obj_create(lv_scr_act());
    lv_obj_remove_style_all(ui_container);
    lv_obj_set_size(ui_container, 1024, 600);
    lv_obj_set_style_bg_color(ui_container, lv_color_hex(0x000000), 0);
    lv_obj_set_style_bg_opa(ui_container, LV_OPA_COVER, 0);
    lv_obj_set_style_border_opa(ui_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_outline_opa(ui_container, LV_OPA_TRANSP, 0);
    
    // 左侧返回按钮
    back_btn = lv_imgbtn_create(ui_container);
    lv_obj_set_size(back_btn, 64, 64);
    lv_obj_add_event_cb(back_btn, back_event_cb, LV_EVENT_CLICKED, NULL);
    lv_imgbtn_set_src(back_btn, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_img_back_64_png, NULL);
    lv_obj_align(back_btn, LV_ALIGN_TOP_LEFT, 10, 10);

    // 右侧拍照按钮
    photo_vedio_btn = lv_imgbtn_create(ui_container);
    lv_obj_set_size(photo_vedio_btn, 64, 64);
    lv_obj_add_event_cb(photo_vedio_btn, photo_vedio_event_cb, LV_EVENT_ALL, NULL);
    lv_imgbtn_set_src(photo_vedio_btn, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_img_takephotp_64_png, NULL);
    lv_obj_align(photo_vedio_btn, LV_ALIGN_TOP_RIGHT, -10, 268);

    // 右侧查看图片按钮
    image_btn = lv_imgbtn_create(ui_container);
    lv_obj_set_size(image_btn, 64, 64);
    lv_obj_add_event_cb(image_btn, check_image_event_cb, LV_EVENT_CLICKED, NULL);
    lv_imgbtn_set_src(image_btn, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_img_image_64_png, NULL);
    lv_obj_align(image_btn, LV_ALIGN_BOTTOM_RIGHT, -10, -10);
    
    // 更新界面状态
    current_ui_state = UI_STATE_CAMERA;
}

void ui_check_image(void)
{
    close_v4l2_camera();

    clear_area(0, 0, 1024, 600, lv_color_hex(0x000000));
    lv_refr_now(NULL);
    sleep(1);

    image_btn = lv_imgbtn_create(lv_scr_act());
    lv_obj_set_size(image_btn, 64, 64);
    lv_obj_add_event_cb(image_btn, check_image_event_cb, LV_EVENT_CLICKED, NULL);
    lv_imgbtn_set_src(image_btn, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_img_image_64_png, NULL);
    lv_obj_align(image_btn, LV_ALIGN_TOP_LEFT, 950, 526);

    image_prev_btn = lv_imgbtn_create(lv_scr_act());
    lv_obj_set_size(image_prev_btn, 64, 64);
    lv_obj_add_event_cb(image_prev_btn, prev_image_event_cb, LV_EVENT_CLICKED, NULL);
    lv_imgbtn_set_src(image_prev_btn, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_img_image_prev_64_png, NULL);
    lv_obj_align(image_prev_btn, LV_ALIGN_TOP_LEFT, 10, 268);

    image_next_btn = lv_imgbtn_create(lv_scr_act());
    lv_obj_set_size(image_next_btn, 64, 64);
    lv_obj_add_event_cb(image_next_btn, next_image_event_cb, LV_EVENT_CLICKED, NULL);
    lv_imgbtn_set_src(image_next_btn, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_img_image_next_64_png, NULL);
    lv_obj_align(image_next_btn, LV_ALIGN_TOP_LEFT, 950, 268);
    
    // 显示最新照片
    current_photo_index = total_photos - 1;
    printf("current_photo_index = %d\n", current_photo_index);
    show_current_photo();
    
    // 更新界面状态
    current_ui_state = UI_STATE_IMAGE_VIEW;
}